<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">
$(function(){
    getTaskData(1);
})

function getTaskData(num){
    var url = "/phoneImport/queryTask/" + num;
    AjaxUtil.ajaxRequest(url, "json", function (allData) {
        setTaskList(allData);//构建
    },false);
}

/*刷新CaptionHover插件*/
function initCaptionHover(){
  $('.demo').captionHover({
    fx: 'sophie'
  });
}

/*构建前台界面+数据*/
function setTaskList(allData){
    $(".content-inner").empty();
    setTaskListButton(allData);
    setTaskListData(allData);
    initCaptionHover();
}

/*构建前台界面*/
function setTaskListButton(allData){
    var prePage = allData.prePage;
    var nextPage = allData.nextPage;
    var hasPreviousPage = allData.hasPreviousPage;
    var hasNextPage = allData.hasNextPage;
    if (hasPreviousPage) {
      $(".content-inner").append("<div class='prev_button' onclick='moveToTaskList("+prePage+");'></div>");
    }
    
    if (hasNextPage) {
      $(".content-inner").append("<div class='next_button' onclick='moveToTaskList("+nextPage+");'></div>"); 
    }
}

/*构建前台数据*/
function setTaskListData(allData){
    var dataList = allData.list;
    var taskListHtml = "";
    for(var i=0;i<7;i++){
        var data = dataList[i];
        if(data != undefined){
            var id = noNull(data.id);//任务id
            var license = noNull(data.license);//批准文号
            var account = noNull(data.account);//手机号
            var start_Time = noNull(data.start_Time);//开始时间
            var end_Time = noNull(data.end_Time);//结束
            taskListHtml += getTaskListHtml(i,id,license,account,start_Time,end_Time);
        } else {
            taskListHtml += getNoTaskListHtml();
        }
    }
    $(".content-inner").append(taskListHtml);
    if(dataList.length>0){
        choose(0,dataList[0].id);
    }
    $("#id").val(dataList[0].id); //隐藏域赋值
}

/*任务列表为空*/
function getNoTaskListHtml(){
  var taskListHtml = 
  "<div class='content-inner-task' style='background-image:url(\"../img/index/about-bg.jpg\");'></div>";
  return taskListHtml;
}

/*构建tasklisthtml*/
function getTaskListHtml(index,id,license,account,start_Time,end_Time){
    var taskListHtml = 
    "<div class='content-inner-task' onclick='choose(" + index + "," + id + ");' style='background-image:url(\"../img/index/about-bg.jpg\");'>" +
    "  <figure class='demo'>" +
    "  <figcaption style='height:150px;'>" +
    "    <div style='margin-top:20px;'>" +
    "      <div style='padding:0 10px 0 15px;text-align:left;'>" +
    "  <div class='task-body-title'>"+
    "   <span class='task-body-title-span'><@spring.message 'phoneSYImport.mode.id'/>:</span>"+
    "  </div>"+
    "  <div class='task-body-info'>"+
    "   <span class='task-body-info-span'>"+noNull(id)+"</span>"+
    "  </div>"+
    "  <div class='task-body-title'>"+
    "   <span class='task-body-title-span'><@spring.message 'phoneSYImport.mode.approved'/>:</span>"+
    "  </div>"+
    "  <div class='task-body-info'>"+
    "   <span class='task-body-info-span'>"+noNull(license)+"</span>"+
    "  </div>"+
    "  <div class='task-body-title'>"+
    "   <span class='task-body-title-span'><@spring.message 'phoneSYImport.mode.phone'/>:</span>"+
    "  </div>"+
    "  <div class='task-body-info'>"+
    "   <span class='task-body-info-span'>"+noNull(account)+"</span>"+
    "  </div>"+
    "  <div class='task-body-title'>"+
    "   <span class='task-body-title-span'> <@spring.message 'phoneSYImport.mode.begin.time'/>:</span>"+
    "  </div>"+
    "  <div class='task-body-info'>"+
    "   <span class='task-body-info-span'>"+noNull(start_Time)+"</span>"+
    "  </div>"+
    "  <div class='task-body-title'>"+
    "   <span class='task-body-title-span'><@spring.message 'phoneSYImport.mode.end.time'/>:</span>"+
    "  </div>"+
    "  <div class='task-body-info'>"+
    "   <span class='task-body-info-span'>"+noNull(end_Time)+"</span>"+
    "  </div>"+
    "      </div>" +
    "    </div>" +
    "  </figcaption>" +
    "  </figure>"+
    "</div>";
    return taskListHtml;
}

/*翻页*/
function moveToTaskList(num){
    getTaskData(num);
}

//临时存储marker
var markers = [];

/*构建地图*/
function initMap(){
    map = buildMap("googleMap");
    var right = document.getElementById("button_right");//自定义工具栏 (右)
    $("#button-right-father").append($("#button-right-father").html());
    buildControl(right,"right");
    //设置点击监听
    google.maps.event.addListener(map, 'click', function (event) {
        placeMarker(event.latLng);
    });
}

//构建点并且记录
function placeMarker(location) {
    var marker = buildMarkerAndLabelByPoint(location,"",(markers.length+1)+"");
    markers.push(marker);
    settingButton();
}

/*删除上一个点*/
function deleteLastMarker(){
    var theLastMarker = markers.pop();
    theLastMarker.setMap(null);
    settingButton();
}

/*设置按钮点击状态*/
function settingButton() {
	var length = markers.length;
	if (length == 0) {
		$("#button_right").children("button").attr('disabled',true);
	} else {
		$("#button_right").children("button").attr('disabled',false);
	}
}

/*确认添加*/
function submitMarker(){
    
    //console.log("共有点 "+markers.length+" 个");
    var positions = [];
    for(var index in markers){
        var marker = markers[index];
        var lng = marker.getPosition().lng(); //经度
        var lat = marker.getPosition().lat(); //纬度
        var address = getFormatted_address(lat,lng);
        console.log("下标:" + index + " x="+lng+" ,y="+lat+" ,address="+address);
        var position={"x":lng,"y":lat,"address":address};
        positions.push(position);
    }
    var id = $("#id").val(); //隐藏域取值
    $.ajax({
        type:"POST",
        url: "/phoneImport/modifyResultPosition/"+id,
        //dataType:"json",
        contentType:"application/json", // 指定这个协议很重要
        data:JSON.stringify(positions), //只有这一个参数，json格式，后台解析为实体，后台可以直接用
        success:function(data){
        	layui.use('layer', function(){
    	      var layer = layui.layer;
    	      if (data.result == 1){
    	        layer.alert("<@spring.message 'phoneSYImport.save.success1'/>"+data.total+"<@spring.message 'phoneSYImport.save.success2'/>");
              }else{
                  layer.alert("<@spring.message 'phoneSYImport.save.error'/>");
              }
    	    });
        	closeCover();
        },
        error:function(msg){
        	console.log(msg);
        }
    });
    openCover();
}

/*打开地图遮罩层*/
function openCover(){
    $(".import-cover").removeClass("import-hide");
}

/*关闭地图遮罩层*/
function closeCover(){
    $(".import-cover").addClass("import-hide");
}


/*选择任务*/
function choose(index,id) {
    addBeChoose(index);
	initMap();
	markers = [];
	$("#id").val(id); //隐藏域赋值
}

/*任务栏被选中*/
function addBeChoose(index){
    $(".content-inner-task").removeClass("taskBeChoose");
    $(".content-inner-task:eq("+index+")").addClass("taskBeChoose");
}

</script>

</body>
</html>